package com.agan.aganjavaspyder.crawler.un_demo;

import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpPostRequest;
import com.geccocrawler.gecco.scheduler.SchedulerContext;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cglovewx
 */
@PipelineName("UNSecondPhonePipeline")
@Slf4j
public class UNSecondPhonePipeline implements Pipeline<SecendCtPhone> {

	@SneakyThrows
	@Override
	public void process(SecendCtPhone secendCtPhone) {

		log.info("当前页数:{},总页数：{}",secendCtPhone.getStartPage(),secendCtPhone.getTotleSize());

		for (String  href :secendCtPhone.getResultText()){
			System.out.println("UNSecondPhonePipeline:-----"+href);
			break;
		}
		int currPage = secendCtPhone.getStartPage();
		int totleSize = secendCtPhone.getTotleSize();
		System.out.println("totleSize:----"+totleSize);

		int sizeNum = 12;
		int totlePageNum = secendCtPhone.getTotleSize()/sizeNum + 1;


		if(currPage > totlePageNum){
			log.info("页数大于，跳出处理");
			return;
		}

		//继续抓取下一页构造下一页
		//if (currPage < totalPage) {
		if (currPage < totlePageNum) {
			int nextPage = currPage + 1;
			String nextUrl = "http://s.10010.com/SearchApp/chseSearchList/refresh?"+nextPage;
			HttpPostRequest req = new HttpPostRequest();
			Map<String, String> fields = new HashMap<>();
			//多一页pc_s:1 多一个
			fields.put("serAttrs", getSerAttrs(currPage));
			fields.put("keyword",secendCtPhone.getKeyWord());
			fields.put("categories","");
			fields.put("sortMode","salesVolume");
			fields.put("page.webPager.action",String.valueOf(nextPage));
			fields.put("page.webPager.pageInfo.totalSize",String.valueOf(totleSize));
			fields.put("page.webPager.currentPage",String.valueOf(currPage));
			req.setFields(fields);
			req.setUrl(nextUrl);
			SchedulerContext.into(req);
		}

/*		String resultText = parseString(startCtPhone.getResultText());
		JSONObject jsonObject = JSONUtil.parseObj(resultText);
		System.out.println("jsonObject------"+jsonObject.toString());
		JSONObject dataObject = JSONUtil.parseObj(jsonObject.get("dataObject"));
		System.out.println("dataObject------"+dataObject);
		//处理页信息
		PageInfo pageInfo = JSONUtil.parseObj(dataObject.get("pageInfo")).toBean(PageInfo.class);
		int currPage = startCtPhone.getPageNum();
		int totalPage =  pageInfo.getAllPages();
		log.info("当前页数:{}",currPage);
		if(currPage > totalPage){
			log.info("页数大于，跳出处理");
			return;
		}
		JSONArray proList = JSONUtil.parseArray(dataObject.get("proList"));
		List<PhoneList> phoneLists = proList.toList(PhoneList.class);
		//单页所有手机
		for (PhoneList phone : phoneLists) {
			System.out.println(phone.getXsj() + "----" + phone.getLongTitle() + "-----" + phone);
			if(CollUtil.isNotEmpty(phone.getGroupProd())){
				//手机规格详情
				List<PhoneGroupProd> prods= phone.getGroupProd();
				for (PhoneGroupProd prod : prods){
					//入库对象
					String detilUrl = "http://www.189.cn/products/"+prod.getSalesCode()+".html?internal_search=1";
					//继续抓取详情页
					HttpRequest sub = startCtPhone.getRequest().subRequest(detilUrl);
					SchedulerContext.into(sub);
					Thread.sleep(1000);
				}

			}
		}
		//继续抓取下一页构造下一页
		//if (currPage < totalPage) {
		if (currPage < 2) {
			int nextPage = currPage + 1;
			String nextUrl = "http://www.189.cn/dqmh/tianyiMall/searchMallAction.do?method=goToSearch&searchtext="+startCtPhone.getSearchtext()+"&pageSize=16&internal_search=1&shopId=10001&currentPage="+nextPage+"&listType=0";
			HttpRequest request = startCtPhone.getRequest();
			SchedulerContext.into(request.subRequest(nextUrl));
		}*/

	}
	private static String getSerAttrs(int currPage){
		StringBuilder sb = new StringBuilder("pc_s:1");
		for (int i = 0; i< currPage -1; i++){
			sb.append(";pc_s:1");
		}
		return sb.toString();
	}

	public static void main(String[] args) {

		System.out.println(getSerAttrs(2));
	}



}